/**
 * 
 * @Author: fish
 */
// index.js

import util from '../../utils/util.js';

//获取应用实例
var app = getApp();

var interval;
var varName;
var ctx = wx.createCanvasContext('canvasArcCir');


Page({
  /**
   * 页面的初始数据
   */
  data: {
    siteInfo: [],
    banners: [],
    bannersX: [],
    notices: [],
    goodProducts: [],
    hotProducts: [],
    recentlyProducts: [],
    userInfo: [],
    carstorelist:[],
    adlistr:[],
    progress_txt: '预约中...', 
    YDshowView: false,
    GDshowView: false,
    //flow类型
    //0 流程1
    //1 流程2
    flow:0,

    //flowtype
    //1 阶段1
    //2 阶段2
    //3 阶段3
    //4 阶段4
    //5 阶段5
    
    flowtype:1,

    flow1:false,
    flow2: false,
    flow3: false,
    flow4: false,
    flow5: false,
    orderInfo:'',
  },
  
  drawProgressbg: function () {
    // 使用 wx.createContext 获取绘图上下文 context
    var ctx = wx.createCanvasContext('canvasProgressbg')
    ctx.setLineWidth(4);// 设置圆环的宽度
    ctx.setStrokeStyle('#20183b'); // 设置圆环的颜色
    ctx.setLineCap('round') // 设置圆环端点的形状
    ctx.beginPath();//开始一个新的路径
    ctx.arc(110, 110, 100, 0, 2 * Math.PI, false);
    //设置一个原点(110,110)，半径为100的圆的路径到当前路径
    ctx.stroke();//对当前路径进行描边
    ctx.draw();
  },
  onReady: function () {
    this.drawProgressbg();
  },

  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    wx.showLoading({
      title: '加载中...'
    });
    this.getlist();
    wx.hideLoading();
    this.getAd();
    
  },

  showDetail: function (obj){
      var that = this;
    var objkey = parseInt(obj.currentTarget.id);
    var ret = this.data.carstorelist[objkey];
    let data = JSON.stringify(ret);
    wx.navigateTo({
      url: '../carstore/detail/detail?data=' + data,
    });
  },


  getlist() {
    var url = app.globalData.SERVER+'basedata/getFilteredShops';
    var lon = app.globalData.lon;
    var lat = app.globalData.lan;
    util.request(url, 'POST', {
      key:'',
      lon: lon,
      lan: lat,
      type: '0',
      sortType: '1',
    }).then(res => {
      util.showToast('成功', 'error', 2500);
    }).catch(err => {
      if (err.data.code === 1) {
        this.setData({
          carstorelist: err.data.ret,
        });
        var num = 1;
      } else {
        self.setData({
          siteInfo: err.data.site_info
        });
      }


    });
  },


  // 首页展示产品
  // ecapi.home.product.list
  getPorducts() {
    // util.request(util.apiUrl + 'ecapi.home.product.list', 'POST').then(res => {
    // var url = util.myurl + 'indexList.php';
    // util.request(url, 'POST').then(res => {
    // // util.request('http://127.0.0.01:8888/myapp/insertUser.php', 'POST').then(res => {
    
    //   this.setData({
    //     goodProducts: res.good_products
    //   });
    // });
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 页面渲染完成
   
  
  },

  drawCircle: function (step) {
    var context = wx.createCanvasContext('canvasProgress');
    // 设置渐变
    var gradient = context.createLinearGradient(200, 100, 100, 200);
    gradient.addColorStop("0", "#2661DD");
    gradient.addColorStop("0.5", "#40ED94");
    gradient.addColorStop("1.0", "#5956CC");

    context.setLineWidth(10);
    context.setStrokeStyle(gradient);
    context.setLineCap('round')
    context.beginPath();
    // 参数step 为绘制的圆环周长，从0到2为一周 。 -Math.PI / 2 将起始角设在12点钟位置 ，结束角 通过改变 step 的值确定
    context.arc(110, 110, 100, -Math.PI / 2, step * Math.PI - Math.PI / 2, false);
    context.stroke();
    context.draw()
  },
  onReady: function () {
    this.drawProgressbg();
    this.drawCircle(0.5)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 页面显示
    // util.updateCartNum();
    this.getYdlist();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    // 页面隐藏
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.siteInfo.name,
      path: '/pages/index/index',
      success(e) {
        // 需要在页面onLoad()事件中实现接口
        wx.showShareMenu({
          // 要求小程序返回分享目标信息
          withShareTicket: true
        });
      },
      fail(e) {
      },
      complete() { }
    }
  },

  
  getAd() {
    var url = app.globalData.SERVER + 'adv/query';

    var that = this;
    util.request(url, 'POST', {
      type: '1',
    }).then(res => {
      util.showToast('成功', 'error', 2500);
    }).catch(err => {
      if (err.data.code === 1) {
        that.setData({
          adlistr: err.data.ret,
        });
        var num = 1;
      } else {
        self.setData({
          adlistr: err.data.ret
        });
      }
    });
  },

  getYdlist() {
    
    // var usetinfo = wx.getStorageSync("user_info");
    var usetinfo = util.getData("userinfo");
    if (usetinfo == '') 
    {
      return;
    }

    var url = app.globalData.SERVER + 'contact/getLastestOrderByCarer';

    var usetinfo = wx.getStorageSync("user_info");
    var that = this;

    var usetinfo_id = usetinfo._id;

    var status = this.data.type;
    util.request(url, 'POST', {
      carerid: usetinfo_id,
    }).then(res => {
      util.showToast('成功', 'error', 2500);
    }).catch(err => {
      if (err.data.code === 1) {
        console.log(err.data.ret);
        if (err.data.ret.length == 0)
        {
          that.setData({
            ydtype:'1',
            infolist: err.data.ret,
          });
          that.getGDlist('');
        }else{
          var state = err.data.ret.state;
          // state = 2;
        
          if (state == 0)
          {
            that.setData({
              flow: '1',
              flowtype :'1',
              YDshowView: true,
              flow1: true,
            });
            
          }else if( state==1)
          {
            that.setData({
              flow: '1',
              flowtype: '2',
              YDshowView: true,
              flow1: true,
              flow2: true,
            });
          }else if(state==2){
            that.setData({
              flow: '1',
              flowtype: '3',
              YDshowView: true,
              flow1: true,
              flow2: true,
              flow3:true,
            });
            that.getGDlist(err.data.ret._id);
          }
        that.setData({
          flow:'2',
          infolist: err.data.ret,
        });
       
       }
      } else {
       
        that.setData({
          flow: '2',
        });
        that.getGDlist('');
      }
      

    });
  },

  getGDlist(orderid) {
    var url = app.globalData.SERVER + 'repair/queryOneLastestByCarer';

    // var usetinfo = wx.getStorageSync("user_info");
    var usetinfo = util.getData("userinfo");
    var that = this;

    var usetinfo_id = usetinfo._id;

    var status = this.data.type;
    util.request(url, 'POST', {
      carerid: usetinfo_id,
    }).then(res => {
      util.showToast('成功', 'error', 2500);
    }).catch(err => {
      if (err.data.code === 1) {
        console.log('11111');
        console.log(err.data.ret);
        if (err.data.ret.length == 0) {
           if(orderid=='')
           {
             that.setData({
               YDshowView: false,
               GDshowView: false,
             });
           }else{
             that.setData({
               YDshowView: true,
               GDshowView: false,
             });
           }
        } else {
          if (orderid == err.data.ret.orderid && orderid !='')
          {

            var orderstate = err.data.ret.state;
            // orderstate =1;
            if (orderstate ==0)
            {
              that.setData({
                flow: '1',
                flowtype: '3',
                YDshowView: true,
                GDshowView: false,
                flow1: true,
                flow2: true,
                flow3: true,
                orderInfo: err.data.ret,
              });

            } else if (orderstate == 1) {
              that.setData({
                flow: '1',
                flowtype: '4',
                YDshowView: true,
                GDshowView: false,
                flow1: true,
                flow2: true,
                flow3: true,
                flow4: true,
                orderInfo: err.data.ret,
              });

            } else if (orderstate == 2) {
              that.setData({
                flow: '1',
                flowtype: '5',
                YDshowView: false,
                GDshowView: false,
                flow1: true,
                flow2: true,
                flow3: true,
                flow4: true,
                flow5: true,
                orderInfo: err.data.ret,
              });

            }

            
          }else{
                that.setData({
                  flow: '2',
                YDshowView: false,
                GDshowView:true,
                });
          }

        }
      }


    });

  },

  bindOrderInfo: function () {
      var type = this.data.flowtype;
    if (type == 1 || type == 2)
      {
        wx.navigateTo({
          url: '../schedule/schedulelist/schedulelist',
        });
      } else if (type == 3 || type == 4 || type == 5 ){
        var that = this;
        var ret = this.data.orderInfo;
        let data = JSON.stringify(ret);
        wx.navigateTo({
          url: '../workorder/workorderdetail/workorderdetail?data=' + data,
        });
      }
  },
});


